<?php 
set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
include 'config.php';
require_once './Classes/PHPExcel.php';
 
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '姓名')
->setCellValue('B1', '手机号')
->setCellValue('C1', '省')
->setCellValue('D1', '市')
->setCellValue('E1', '微信昵称')
->setCellValue('F1', '注册时间')
->setCellValue('G1', '一等奖')
->setCellValue('H1', '二等奖开门冰箱')
->setCellValue('I1', '三等奖空气净化器')
->setCellValue('J1', '四等奖高尔夫雨伞')
->setCellValue('K1', '五等奖精美礼品')
->setCellValue('L1', '来源')
;
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
$sql="SELECT * FROM `uinfo` order by id desc";
$query =$mysqli->query($sql);
$index=2;
$jT=array(1=>'一等奖', 2=>'二等奖开门冰箱', 3=>'三等奖空气净化器',4=>'四等奖高尔夫雨伞',5=>'五等奖精美礼品');
while ($list=$query->fetch_array()){
    $sql="SELECT nickname FROM `users` where openid='".$list['openid']."' limit 0,1 ";
    $queryUser =$mysqli->query($sql);
    $userInfo=$queryUser->fetch_assoc();
    
    $laizi=$list['laizi'];
    if('2019' == $laizi){
        $laizi='柏尔地板';
    }else{
        $laizi='德尔地板';
    }
    
    $jg=$jh=$ji=$jj=$jk=0;
    
    $sql="SELECT jp FROM `chou` where openid='".$list['openid']."' ";
    $queryJ =$mysqli->query($sql);
    while ($listJ = $queryJ->fetch_array()){
        if (1 == $listJ['jp']){
            $jg++;
        }
        if (2 == $listJ['jp']){
            $jh++;
        }
        if (3 == $listJ['jp']){
            $ji++;
        }
        if (4 == $listJ['jp']){
            $jj++;
        }
        if (5 == $listJ['jp']){
            $jk++;
        }
    }
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$index, $list['name'])
    ->setCellValue('B'.$index, $list['phone'])
    ->setCellValue('C'.$index, $list['sheng'])
    ->setCellValue('D'.$index, $list['shi'])
    ->setCellValue('E'.$index, base64_decode($userInfo['nickname']))
    ->setCellValue('F'.$index, $list['createtime'])
    ->setCellValue('G'.$index, $jg)
    ->setCellValue('H'.$index, $jh)
    ->setCellValue('I'.$index, $ji)
    ->setCellValue('J'.$index, $jj)
    ->setCellValue('K'.$index, $jk)
    ->setCellValue('L'.$index, $laizi)
    ;
    $index++;
    
     
    
    
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户信息');
 
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户信息.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//$objWriter->save();
exit;